package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.ExpertExplain;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.ui.activity.ExplainDetailActivity;
import com.qilu.pe.ui.activity.ReportDetailActivity;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.SizeUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 专家解读fragment
public class ExpertExplainFragment extends BaseFragment {
    private RecyclerView rcv_list;// 专家解读列表
    private TextView empty_view;

    private ExpertExplainListAdapter adapter;
    private List<ExpertExplain> explainList = new ArrayList<>();// 专家解读数据

    private String status = "";// 区分解读状态 0 待解读 1 已解读

    public ExpertExplainFragment() {

    }

    public static ExpertExplainFragment newInstance(String status) {
        ExpertExplainFragment fragment = new ExpertExplainFragment();
        Bundle args = new Bundle();
        args.putString("status", status);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        status = getArguments().getString("status");
        adapter = new ExpertExplainListAdapter(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_expert_explain, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);
        empty_view = v.findViewById(R.id.empty_view);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);
        adapter.setDataList(explainList);

        hideProgress();
        reqExpertReadList();
        showProgress();

        return v;
    }

    // 专家解读列表
    private Disposable reqExpertReadList() {
        return APIRetrofit.getDefault().reqExpertReadList(Global.HEADER, status, 1, 999999)
                .compose(RxUtil.<BaseResult2<List<ExpertExplain>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ExpertExplain>>>() {
                    @Override
                    public void accept(BaseResult2<List<ExpertExplain>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {

                        } else {

                        }
                        explainList = listBaseResult2.getData();
                        adapter.setDataList(explainList);
                        if (isDataEmpty(explainList)) {
                            empty_view.setVisibility(View.VISIBLE);
                        } else {
                            empty_view.setVisibility(View.GONE);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        empty_view.setVisibility(View.VISIBLE);
                    }
                });
    }

    private class ExpertExplainListAdapter extends ListBaseAdapter<ExpertExplain> {

        public ExpertExplainListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_expert_explain_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            CardView cv_item = holder.getView(R.id.cv_item);
            TextView tv_check = holder.getView(R.id.tv_check);
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_num = holder.getView(R.id.tv_num);
            TextView tv_patient_name = holder.getView(R.id.tv_patient_name);
            TextView tv_expert_name = holder.getView(R.id.tv_expert_name);
            TextView tv_date = holder.getView(R.id.tv_date);

            if (TextUtils.equals("1", status)) {// 已解读
                tv_check.setText("查看解读");
            } else {
                tv_check.setText("查看报告");
            }
            tv_check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (TextUtils.equals("1", status)) {// 已解读
                        startActivity(ExplainDetailActivity.class);
                    } else {
                        startActivity(ReportDetailActivity.class);
                    }
                }
            });

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) cv_item.getLayoutParams();
            if (position == 0) {
                params.topMargin = SizeUtils.dp2px(10);
                params.bottomMargin = SizeUtils.dp2px(5);
            } else if (position == mDataList.size() - 1) {
                params.topMargin = SizeUtils.dp2px(5);
                params.bottomMargin = SizeUtils.dp2px(10);
            } else {
                params.topMargin = SizeUtils.dp2px(5);
                params.bottomMargin = SizeUtils.dp2px(5);
            }

            ExpertExplain explain = mDataList.get(position);
            tv_name.setText(explain.getProject_name());
            tv_num.setText(explain.getAbnormal() + "项异常");
            tv_patient_name.setText(explain.getName());
            tv_expert_name.setText(explain.getEname());
            tv_date.setText("解读时间:" + explain.getCreated_at());
        }
    }
}